package com.sne.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.entity.Result;
import com.sne.sys.entity.SysDictItem;
import com.sne.sys.service.ISysDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 * 字典项 前端控制器
 * </p>
 *
 * @author fanghh
 * @since 2020-01-10
 */
@Slf4j
@Valid
@RestController
@RequestMapping("/sys/sys-dict-item")
@Api(tags = "字典项")
public class SysDictItemController {

    @Autowired
    private ISysDictItemService iSysDictItemService;

    @ApiOperation(value = "获取字典项分页列表")
    @PostMapping("/page")
    public Result page(Page page, @RequestBody SysDictItem dictItem) {
        return Result.success(iSysDictItemService.pageDictItem(page, dictItem));
    }


    @ApiOperation(value = "保存字典项")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SysDictItem dictItem) {
        if (iSysDictItemService.checkDictUnique(dictItem)) {
            return Result.failed("字典项编号已存在",null);
        }
        return Result.success(iSysDictItemService.save(dictItem));
    }

    @ApiOperation(value = "更新字典项")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody SysDictItem dictItem) {
        if (iSysDictItemService.checkDictUnique(dictItem)) {
            return Result.failed("字典项编号已存在",null);
        }
        return Result.success(iSysDictItemService.updateById(dictItem));
    }

    @ApiOperation(value = "根据id获取字典项")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id") Long id) {
        return Result.success(iSysDictItemService.getById(id));
    }


    @ApiOperation(value = "根据字典组code，字典项值获取")
    @GetMapping("/find/{code}/{itemCode}")
    public Result findDictItemByCode(@PathVariable("code") String code, @PathVariable("itemCode") String itemCode) {
        return Result.success(iSysDictItemService.findDictItemCode(code, itemCode));
    }

    @ApiOperation(value = "删除字典组")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(iSysDictItemService.removeById(id));
    }

    @ApiOperation(value = "批量删除字典组")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1,message = "ids不能为空") List<Long> ids) {
        return Result.success(iSysDictItemService.removeByIds(ids));
    }

    @ApiOperation(value = "根据字典组编号获取字典项")
    @GetMapping("/findDictItemByCode/{code}")
    public Result findDictItemByCode(@PathVariable String code){
        return Result.success(iSysDictItemService.findDictItemCode(code));
    }

}
